#include "Game.hh" // 包含游戏头文件，定义了Game类和相关功能 / Include Game header, defines Game class and related functions
#include <cstdlib> // 包含标准库，用于生成随机数 / Include standard library for generating random numbers
#include <map> // 包含映射库，用于存储Creeper的爆炸倒计时 / Include map library for storing Creeper explosion countdowns

// 更新敌人技能：让当前房间的敌人使用特殊技能攻击玩家
// Update enemy skills: Make enemies in the current room use special skills to attack the player
void Game::updateEnemySkills() {
    if (inVillage) return; // 如果玩家在村庄，敌人不使用技能，直接退出 / If player is in a village, enemies don't use skills, exit function

    auto& enemies = currentRoom->getEnemies(); // 获取当前房间的敌人列表 / Get the list of enemies in the current room
    for (auto it = enemies.begin(); it != enemies.end(); ) { // 遍历敌人列表 / Loop through the enemy list
        Character* enemy = static_cast<Character*>(*it); // 获取当前敌人对象 / Get the current enemy object
        if (enemy->getName() == "Creeper") { // 如果敌人是Creeper（苦力怕） / If enemy is a Creeper
            if (creeperCountdown.find(enemy) == creeperCountdown.end()) creeperCountdown[enemy] = 3; // 如果Creeper没有倒计时，初始化为3回合 / If Creeper has no countdown, initialize to 3 turns
            if (creeperCountdown[enemy] > 0) { // 如果倒计时大于0 / If countdown is greater than 0
                addMessage("[Turn " + std::to_string(turnCount) + "] Creeper hisses, countdown: " + std::to_string(creeperCountdown[enemy])); // 添加消息：Creeper发出嘶嘶声，显示剩余倒计时 / Add message: Creeper hisses, show remaining countdown
                creeperCountdown[enemy]--; // 倒计时减1 / Decrease countdown by 1
            } else { // 如果倒计时达到0 / If countdown reaches 0
                addMessage("[Turn " + std::to_string(turnCount) + "] Creeper explodes, dealing massive damage!"); // 添加消息：Creeper爆炸，造成巨大伤害 / Add message: Creeper explodes, dealing massive damage
                if (player->takeDamage(50) && !player->isAlive()) { // 玩家受到50点伤害，检查是否死亡 / Player takes 50 damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
                delete enemy; // 释放Creeper的内存（爆炸后消失） / Free Creeper's memory (disappears after explosion)
                it = enemies.erase(it); // 从敌人列表移除Creeper / Remove Creeper from enemy list
                creeperCountdown.erase(enemy); // 移除Creeper的倒计时记录 / Remove Creeper's countdown record
                continue; // 继续循环，跳过后续代码 / Continue loop, skip remaining code
            }
        } else if (enemy->getName() == "Skeleton") { // 如果敌人是Skeleton（骷髅） / If enemy is a Skeleton
            if (rand() % 2 == 0) { // 以50%概率触发技能 / 50% chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] Skeleton shoots an arrow!"); // 添加消息：骷髅射箭 / Add message: Skeleton shoots an arrow
                if (player->takeDamage(enemy->getAttack() + 5) && !player->isAlive()) { // 玩家受到骷髅攻击力+5的伤害，检查是否死亡 / Player takes Skeleton's attack + 5 damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        } else if (enemy->getName() == "Witch") { // 如果敌人是Witch（女巫） / If enemy is a Witch
            if (rand() % 3 == 0) { // 以1/3概率触发技能 / 1/3 chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] Witch throws a poison potion!"); // 添加消息：女巫投掷毒药 / Add message: Witch throws a poison potion
                if (player->takeDamage(15) && !player->isAlive()) { // 玩家受到15点伤害，检查是否死亡 / Player takes 15 damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        } else if (enemy->getName() == "Zombie" || enemy->getName() == "Baby Zombie") { // 如果敌人是Zombie或Baby Zombie（僵尸或小僵尸） / If enemy is a Zombie or Baby Zombie
            if (rand() % 2 == 0) { // 以50%概率触发技能 / 50% chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] " + enemy->getName() + " lunges at you!"); // 添加消息：僵尸扑向你 / Add message: Zombie lunges at you
                if (player->takeDamage(enemy->getAttack()) && !player->isAlive()) { // 玩家受到僵尸的攻击力伤害，检查是否死亡 / Player takes Zombie's attack damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        } else if (enemy->getName() == "Enderman") { // 如果敌人是Enderman（末影人） / If enemy is an Enderman
            if (rand() % 2 == 0) { // 以50%概率触发技能 / 50% chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] Enderman teleports and attacks!"); // 添加消息：末影人传送并攻击 / Add message: Enderman teleports and attacks
                if (player->takeDamage(enemy->getAttack()) && !player->isAlive()) { // 玩家受到末影人的攻击力伤害，检查是否死亡 / Player takes Enderman's attack damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        } else if (enemy->getName() == "Ender Dragon") { // 如果敌人是Ender Dragon（末影龙） / If enemy is an Ender Dragon
            if (rand() % 2 == 0) { // 以50%概率触发技能 / 50% chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] Ender Dragon breathes fire!"); // 末影龙喷火 / Add message: Ender Dragon breathes fire
                if (player->takeDamage(enemy->getAttack() + 10) && !player->isAlive()) { // 玩家受到末影龙攻击力+10的伤害，检查是否死亡 / Player takes Ender Dragon's attack + 10 damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        } else if (enemy->getName() == "Pillager") { // 如果敌人是Pillager（掠夺者） / If enemy is a Pillager
            if (rand() % 2 == 0) { // 以50%概率触发技能 / 50% chance to trigger skill
                addMessage("[Turn " + std::to_string(turnCount) + "] Pillager fires a crossbow!"); // 添加消息：掠夺者发射弩箭 / Add message: Pillager fires a crossbow
                if (player->takeDamage(enemy->getAttack() + 3) && !player->isAlive()) { // 玩家受到掠夺者攻击力+3的伤害，检查是否死亡 / Player takes Pillager's attack + 3 damage, check if dead
                    addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
                    gameOver = true; // 设置游戏结束 / Set game over
                }
            }
        }
        ++it; // 移动到下一个敌人 / Move to the next enemy
    }
}